package com.mrd.schedule.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import com.mrd.schedule.zk.ZKManager.KEYS;

@ConfigurationProperties(prefix = "uncode.schedule", ignoreInvalidFields = true)
public class UncodeScheduleConfig {

  private String zkConnect;
  private String rootPath = "/uncode/schedule";
  private int zkSessionTimeout = 60000;
  private String zkUsername;
  private String zkPassword;
  private List<String> ipBlackList;


  public Map<String, String> getConfig() {
    Map<String, String> properties = new HashMap<String, String>();
    properties.put(KEYS.zkConnectString.key, zkConnect);
    if (StringUtils.isNotBlank(rootPath)) {
      properties.put(KEYS.rootPath.key, rootPath);
    }
    if (zkSessionTimeout > 0) {
      properties.put(KEYS.zkSessionTimeout.key, zkSessionTimeout + "");
    }
    if (StringUtils.isNotBlank(zkUsername)) {
      properties.put(KEYS.userName.key, zkUsername);
    }
    if (StringUtils.isNotBlank(zkPassword)) {
      properties.put(KEYS.password.key, zkPassword);
    }
    StringBuilder sb = new StringBuilder();
    if (ipBlackList != null && ipBlackList.size() > 0) {
      for (String ip : ipBlackList) {
        sb.append(ip).append(",");
      }
      ipBlackList.remove(sb.lastIndexOf(","));
    }
    properties.put(KEYS.ipBlacklist.key, sb.toString());
    return properties;
  }


  public String getZkConnect() {
    return zkConnect;
  }

  public void setZkConnect(String zkConnect) {
    this.zkConnect = zkConnect;
  }

  public String getRootPath() {
    return rootPath;
  }

  public void setRootPath(String rootPath) {
    this.rootPath = rootPath;
  }

  public int getZkSessionTimeout() {
    return zkSessionTimeout;
  }

  public void setZkSessionTimeout(int zkSessionTimeout) {
    this.zkSessionTimeout = zkSessionTimeout;
  }

  public String getZkUsername() {
    return zkUsername;
  }

  public void setZkUsername(String zkUsername) {
    this.zkUsername = zkUsername;
  }

  public String getZkPassword() {
    return zkPassword;
  }

  public void setZkPassword(String zkPassword) {
    this.zkPassword = zkPassword;
  }

  public List<String> getIpBlackList() {
    return ipBlackList;
  }

  public void setIpBlackList(List<String> ipBlackList) {
    this.ipBlackList = ipBlackList;
  }

}
